1. Field of the Invention
This invention relates to the field of object-oriented programming. More specifically, the present invention relates to the derivation of a function from another function in a programming language.
Sun, Sun Microsystems, the Sun logo, Solaris and all Java-based trademarks and logos are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. All SPARC trademarks are used under license and are trademarks of SPARC International, Inc. in the United States and other countries. Products bearing SPARC trademarks are based upon an architecture developed by Sun Microsystems, Inc.
2. Background Art
In a traditional programming language, such as Java, C++, or Delphi, the concepts of “functions” and a “classes” are distinct. A function computes some value based on its set of input arguments. A class is used to define a type for use by the program. The distinction between classes and functions causes problems. With respect to functions, they cannot take advantage of class-based options such as inheritance. Furthermore, a funtion cannot be defined from within a function.
Before discussing this problem, it is instructive to summarize the differences between functions and classes in the context of object-oriented programming.
Object Oriented Programming
Object-oriented programming is a method of creating computer programs by combining certain fundamental building blocks, and creating relationships among and between the building blocks. The building blocks object-oriented programming systems are called “objects.” An object is a programming unit that groups together a data structure (instance variables) and the operations (methods) that can use or affect that data. Thus, an object consists of data and one or more operations or procedures that can be performed on that data. The joining of data and operations into a unitary building block is “encapsulation.” In object-oriented programming, operations that can be performed on the data are referred to as “methods.”An object can be instructed to perform one of its methods when it receives a “message.”A message is a command or instruction to the object to execute a certain method. It consists of a method selection (name) and arguments that are sent to an object. A message tells the receiving object what to do.
One advantage of object-oriented programming is the way in which methods are invoked. When a message is sent to an object, it is not necessary for the message to instruct the object how to perform a certain method. It is only necessary to request that the object execute the method. This greatly simplifies program development.
Object-oriented programming languages are generally based on one of two schemes for representing general concepts and sharing knowledge. One scheme is known as the “class” scheme. The other scheme is known as the “prototype” scheme. Both the set-based (“class” scheme) and prototype-based object-oriented programming schemes are generally described in Lieberman, “Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems,” OOPSLA 86 Proceedings, September 1986, pp. 214–223.
In accordance with the embodiments of the present invention, the class-based object-oriented scheme will be described.
Class Scheme
An object that describes behavior is called a “class.” Objects that acquire a behavior and that have states are called “instances.” Thus, in the Java language, a class is a particular type of object. In Java, any object that is not a class object is said to be an instance of its class. An example of a class definition in Java is as follows:
class X {X (int arg);// constructor// class members}This structure defines the class to be an encapsulation of a set of members.
Some of the class members are defined as being “special” because they have the same name as the class itself. These members are known as a constructor. A class member can be either a data member, or a variable, or a function, also known as a method.
Two or more classes form a “hierarchy.” Each subclass in the hierarchy may add to or modify the behavior of the object in question and may also add additional states. Inheritance is a fundamental property of the class scheme and allows objects to acquire behavior from other objects.
The inheritance hierarchy is the hierarchy of classes defined by the arrangement of superclasses and subclasses. Except for the root classes, every class has a superclass, and any class may have an unlimited number of subclasses. Each class inherits from those classes above it in the hierarchy. Thus, a superclass has the ability to pass its characteristics (methods and instance variables) onto its subclasses.
An example of a superclass in Java is:class X {public:int x;// other members of X} ;Then a class derived from the superclass, X, is defined as:class Y : public X {public:// other members of Y} ;Y inherits the accessible members of X. In this example, the class Y has a member ‘int x’ inherited from class X.
FIG. 1 is a block diagram that illustrates inheritance. Class 1 (generally indicated by block 101) defines a class of objects that have three methods in common, namely, A, B and C. An object belonging to a class is referred to as an “instance” of that class. An example of an instance of class 1 is block 102. An instance such as instance 102 contains all the methods of its parent class. Block 102 contains methods A, B and C.
As discussed, each class may also have subclasses, which also share all the methods of the class. Subclass 1.1 (indicated by block 103) inherits methods A, B and C and defines an additional method, E. Each subclass can have its own instances, such as, for example, instance 104. Each instance of a subclass includes all the methods of the subclass. For example, instance 104 includes methods A, B, C and E of subclass 1.1.
Object-oriented programming languages that utilize the class/instance/inheritance structure described above implement a set-theoretic approach to sharing knowledge. This approach is used in object-oriented programming languages, such as C++, SmallTalk, Delphi and Java.
Functions
A function is a named section of a program that performs a specific task. Some programming languages, such as Java, Delphi and C++, distinguish between a function, which returns a value, and a procedure, which performs some operation but does not return a value. Most programming languages come with a prewritten set of functions that are kept in a library.
A function contains a sequence of instructions and associated variables for performing a particular task. Most languages allow arguments to be passed to the function. Function code is executed from multiple places within software, even from within the function itself. In this case, the function is a recursive function. In Java, a function is defined as follows:
int Z(int arg1, int arg2)    {// statement}This code block defines a function called “Z” that returns an integer and takes two integer parameters.
Unlike a class, a function body can contain only variable declarations and statements. No other functions can be defined inside the body. Also, a function cannot inherit from another function or class. These differences make the use of functions less than optimal. In particular, functions are one of the building blocks of an object-oriented scheme. Since they are limited, in that they cannot inherit or be defined inside another, it causes programmers to use functions less frequently. This results in more ad-hoc solutions to problems and goes against the principles of object oriented programming.